<?php
/**
 * Класс Controller_Ajax_City отвечает за обработку ajax-запросов, связанных с городами
 * @author Mike
 */
class Controller_Ajax_City extends Controller_Ajax {
	
	public function actionSwitch() {
		switch ($this->arrRequest[2]) {
			case 'auto-suggest' : 
				$this->actionAutoSuggest();
				break;
			default:
				$this->setError(404);
				break;
		}
	}
	
	/**
	 * Autosuggest
	 */
	private function actionAutoSuggest() {
		$objDB = Service_Database::instanse();
		$nameStartWith = isset($_POST['nameStartWith']) ? Service_Escape::absolutelyEscapeString($_POST['nameStartWith']) : '';

		if (empty($nameStartWith)) return $this->makeStandardError('Не получены данные');
		
		$stmt = "SELECT `city`.`city_name`,`city`.`ID_city`,`city`.`url` FROM `city` WHERE `city`.`city_name` LIKE '%" . $nameStartWith . "%' LIMIT 5";
		$citiesArray = $objDB->select($stmt);
		$this->arrReturn = array('result' => 'OK', 'serverAnswer' => $citiesArray);
	}
}
?>